package mytaxis.client.login;

import mytaxis.client.R;
import mytaxis.client.service.ComService;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends Activity {

	
	EditText txtEmail;
	EditText txtPass;
	EditText txtName;
	Button register;
	ProgressDialog pd;
	Thread serverCall;
	String mail;
	String pass;
	String username;
	
	private ComService mBoundService;
	
	

	private ServiceConnection mConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
		    mBoundService = ((ComService.LocalBinder)service).getService();
		}
	
		public void onServiceDisconnected(ComponentName className) {
		    mBoundService = null;
		}
		};
	
		Handler mHandler = new Handler() {
			public void handleMessage(Message msg) {
				boolean result = msg.getData().getBoolean("result");
				if(result){
					Toast.makeText(getApplicationContext(),"User registered",0).show();
					Intent i = new Intent(Register.this, Index.class);
					startActivity(i);
					finish();
				}else{
					Toast.makeText(getApplicationContext(),"Error: Register again", Toast.LENGTH_SHORT).show();
				}
			};
		};	

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		initViews();
		startService(new Intent(Register.this, ComService.class));
		bindService(new Intent(this, ComService.class), mConnection, Context.BIND_AUTO_CREATE);

	}

	private void initViews() {
		txtEmail = (EditText) findViewById(R.id.EditTextEmail);
		txtPass = (EditText) findViewById(R.id.EditTextPass);
		txtName = (EditText) findViewById(R.id.EditTextName);
		register = (Button) findViewById(R.id.btnregister);
		pd = new ProgressDialog(this);

		register.setOnClickListener(new Button.OnClickListener(){
			
			public void onClick(View v) {
				
				if(txtName.getText().toString().equals("") || txtEmail.getText().toString().equals("")|| txtPass.getText().toString().equals("") )Toast.makeText(getApplicationContext(), 
						"Empty user or pass", Toast.LENGTH_SHORT).show();
				else if(!txtEmail.getText().toString().contains("@")){
					Toast.makeText(getApplicationContext(), 
							"Incorrect user", Toast.LENGTH_SHORT).show();
				}
				else{
					mail= txtEmail.getText().toString();
					pass = txtPass.getText().toString();
					username = txtName.getText().toString();
					
					
					pd.setMessage("Connecting to server, this may take a few seconds.");
					pd.setTitle("Register");
					pd.show();
					
					Thread t = new Thread(){
			    		public void run(){
			    				boolean res = mBoundService.register(mail, pass,username);
			    				Message msg = new Message();
								Bundle b = new Bundle();
								b.putBoolean("result", res);
								msg.setData(b);
								mHandler.sendMessage(msg);
								pd.dismiss();
			    		};
				};
				t.start();
			}
		}
		}
			);
	}
}